package com.loft.controller;

import com.loft.R;
import com.loft.comm.MyApp;
import com.loft.comm.util.LogCenter;
import com.loft.controller.baseview.BaseActivity;
import com.loft.controller.renren.RenRenOauthWebView;
import com.loft.threadtask.renren.UserInfoAsynctask;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author heb
 * @class_name HomepageActivity.java
 * @create_date 2012-4-3
 * @version 1.0.0
 */
public class HomepageActivity extends BaseActivity {
	private final String TAG = this.getClass().getName();
	private ViewPager _viewPager;
	private List<View> _viewList;
	private ImageView _arrow_left, _arrow_right;
	private TextView _title;
	private ImageView _userImage;
	private boolean isLogined = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void initView() {
		setContentView(R.layout.homepage);
		_viewPager = (ViewPager) findViewById(R.id.homepage_pageview);
		_arrow_left = (ImageView) findViewById(R.id.homepage_title_arrow_left);
		_arrow_right = (ImageView) findViewById(R.id.homepage_title_arrow_right);
		_title = (TextView) findViewById(R.id.homepage_title);
		_userImage = (ImageView) findViewById(R.id.homepage_user_picture);
		_viewList = new ArrayList<View>();
		View view1 = getLayoutInflater().inflate(R.layout.viewpage_item, null);
		View view2 = getLayoutInflater().inflate(R.layout.viewpage_item, null);
		init_top_viewGroup(view1);
		_viewList.add(view1);
		_viewList.add(view2);
	}

	@Override
	public void setAdapers() {
		_viewPager.setAdapter(new PagerAdapter() {
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return _viewList.size();
			}

			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 == (arg1);
			}

			@Override
			public void destroyItem(ViewGroup container, int position,
					Object object) {
				((ViewPager) container).removeView(_viewList.get(position));
				// Log.d("RESULT", "destroyItem"+position);
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager) container).addView(_viewList.get(position), 0);
				return _viewList.get(position);
			}
		});
	}

	@Override
	public void setListeners() {
		_userImage.setOnClickListener(new ImageView.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!isLogined) {
					Intent intent = new Intent();
					intent.setClass(HomepageActivity.this,
							RenRenOauthWebView.class);
					startActivityForResult(intent,
							MyApp.REQUEST_CODE_HOMEPAGE_TO_RENRENOUATHWEBVIEW);
				}
			}
		});
		_viewPager
				.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

					@Override
					public void onPageSelected(int arg0) {
						switch (arg0) {
						case 0:
							_title.setText(R.string.title_renren);
							_arrow_left.setVisibility(View.GONE);
							_arrow_right.setVisibility(View.VISIBLE);
							break;
						case 1:
							_title.setText(R.string.title_rss);
							_arrow_left.setVisibility(View.VISIBLE);
							_arrow_right.setVisibility(View.GONE);
							break;
						default:
							break;
						}

					}

					@Override
					public void onPageScrolled(int arg0, float arg1, int arg2) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onPageScrollStateChanged(int arg0) {
						// TODO Auto-generated method stub

					}
				});
	}

	private void init_top_viewGroup(View view) {
		String[] renren_item_top = getResources().getStringArray(
				R.array.renren_item_top);
		TextView viewpage_item_text_1_1 = (TextView) view
				.findViewById(R.id.viewpage_item_text_line1_column1);
		TextView viewpage_item_text_1_2 = (TextView) view
				.findViewById(R.id.viewpage_item_text_line1_column2);
		TextView viewpage_item_text_1_3 = (TextView) view
				.findViewById(R.id.viewpage_item_text_line1_column3);
		TextView viewpage_item_text_2_1 = (TextView) view
				.findViewById(R.id.viewpage_item_text_line2_column1);
		TextView viewpage_item_text_2_2 = (TextView) view
				.findViewById(R.id.viewpage_item_text_line2_column2);
		TextView viewpage_item_text_2_3 = (TextView) view
				.findViewById(R.id.viewpage_item_text_line2_column3);
		viewpage_item_text_1_1.setText(renren_item_top[0]);
		viewpage_item_text_1_2.setText(renren_item_top[1]);
		viewpage_item_text_1_3.setText(renren_item_top[2]);
		viewpage_item_text_2_1.setText(renren_item_top[3]);
		viewpage_item_text_2_2.setText(renren_item_top[4]);
		viewpage_item_text_2_3.setText(renren_item_top[5]);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode != RESULT_OK) {
			return;
		}
		switch (requestCode) {
		case MyApp.REQUEST_CODE_HOMEPAGE_TO_RENRENOUATHWEBVIEW:
			LogCenter.getInstance().debug(TAG, "ok");
			MyApp.RENREN_ACCESSTOKEN = data.getStringExtra("Callback");
			showDialog(DIALOG_LOADING);
			UserInfoAsynctask userInfoAsynctask = new UserInfoAsynctask(this,
					R.id.thread_flag_userinfo_get, this.get_dialog());
			userInfoAsynctask.execute();
			break;
		default:
			break;
		}
	}

}
